package conf

import (
	"fmt"
	"strings"
)

type DB struct {
	User     string `yaml:"user"`
	Password string `yaml:"password"`
	Host     string `yaml:"host"`
	Port     int    `yaml:"port"`
	DBName   string `yaml:"dbName"`
	Debug    bool   `yaml:"debug"`
	Source   string `yaml:"source"` //mysql or postgres
}

func (d *DB) DSN() string {
	dbSource := strings.ToLower(d.Source)
	switch dbSource {
	case "mysql":
		return fmt.Sprintf("%s:%s@tcp(%s:%d)/%s?charset=utf8mb4&parseTime=True&loc=Local",
			d.User, d.Password, d.Host, d.Port, d.DBName)
	case "postgres", "pgsql":
		return fmt.Sprintf("host=%s user=%s password=%s dbname=%s port=%d sslmode=disable", d.Host, d.User, d.Password, d.DBName, d.Port)
	}
	return ""
}
func (d *DB) Empty() bool {
	return d.User == "" && d.Password == "" && d.Host == "" && d.Port == 0
}
